package org.xiaom.butler.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.service.BaseService;
import org.xiaom.butler.util.Standard;
import org.xiaom.butler.util.Util;

import com.google.gson.JsonObject;

/**
 * Servlet implementation class HotelServlet
 */
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L; 
	private BaseService hotelService;
	private BaseService departmentService;
	private String field="department";
	public DepartmentServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		this.hotelService = new BaseService(Hotel.class);
		this.departmentService=new BaseService(Department.class);
		super.init();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getRequestURI();
		String function = url.split("\\.")[1];
		String name = url.split("/"+field+"/")[1];
		if (function.equals("do")) { // 请求是个功能性请求
			if ("add.do".equals(name)) {
				add(request, response); 
				goToList(request, response,Util.getQueryParam(request, "hotel", ""), Standard.REDIRECT);
			} else if ("delete.do".equals(name)) {
				Department d=(Department) departmentService.get(Util.getQueryParam(request, "id", ""));
				delete(request, response);
				request.setAttribute("hotel", d.getHotel());
				goToList(request, response,d.getHotel().getId().toString(), Standard.REDIRECT);
			} else if ("edit.do".equals(name)) {
				Department d=(Department) departmentService.get(Util.getQueryParam(request, "id", ""));
				edit(request, response);
				goToList(request, response,d.getHotel().getId().toString(), Standard.REDIRECT);
			} else {
				String msg="没找到页面";
				Util.redirectToErrorPage(request, response, msg);
			}
		} else if (function.equals("jsp")) {// 要求显示页面
			if (name.equals("add.jsp")) {
				goToAdd(request, response, null); 
			} else if (name.equals("list.jsp")) {
				goToList(request, response,Util.getQueryParam(request, "hotel", ""), Standard.FORWARD);
			} else if (name.equals("edit.jsp")) {
				goToEdit(request, response, Standard.FORWARD);
			} else {
				Util.redirectToErrorPage(request, response, "请求的路径不正确");
			}
		}
	}

	private void goToEdit(HttpServletRequest request,
			HttpServletResponse response, boolean forward) {
		Department bean =(Department) departmentService.get(Integer.parseInt(Util.getQueryParam(request, "id", "")));
		request.setAttribute("bean", bean);
		Util.forward(getServletConfig().getServletContext(), request, response,
				"department/edit.jsp", false);
		
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		try {
			String id=Util.getQueryParam(request, "id", "");
			String chName = Util.getQueryParam(request, "ch_name", "");
			String engName = Util.getQueryParam(request, "eng_name", "");
			Integer displayIndex = Integer.parseInt(Util.getQueryParam(request, "display_index", ""));
			String imagePath = Util.getQueryParam(request, "image_path", "");
			
			Department d=(Department) departmentService.get(id);  
			d.setChName(chName);
			d.setDisplayIndex(displayIndex);
			d.setEngName(engName);
			d.setImagePath(imagePath);
			departmentService.saveOrUpdate(d);
		} catch (Exception e) {
			e.printStackTrace();
			Util.redirectToErrorPage(request, response, "保存错误。请检查输入");
		}
	} 
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			Hotel h=(Hotel) hotelService.get(Util.getQueryParam(request,"hotel",""));
			String chName = Util.getQueryParam(request, "ch_name", "");
			String engName = Util.getQueryParam(request, "eng_name", "");
			Integer displayIndex = Integer.parseInt(Util.getQueryParam(request, "display_index", ""));
			String imagePath = Util.getQueryParam(request, "image_path", "");
			Department d=new Department();
			d.setChName(chName);
			d.setEngName(engName);
			d.setDisplayIndex(displayIndex);
			d.setImagePath(imagePath);
			d.setHotel(h);
			
			departmentService.saveOrUpdate(d);
			h.getDepartments().add(d);
		} catch (Exception e) {
			e.printStackTrace();
			Util.redirectToErrorPage(request, response, "保存错误。请检查输入");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		JsonObject jo=new JsonObject();
		try {
			String id = (String) request.getParameter("id");
			departmentService.delete(Integer.parseInt(id));
			jo.addProperty("state", "success");
			response.getWriter().write(jo.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("{state:faild}");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	private void getOne(HttpServletRequest request, HttpServletResponse response) {

	}

	private void goToList(HttpServletRequest request,
			HttpServletResponse response,String hotelId, boolean mode)
			throws ServletException, IOException {
		// response.sendRedirect(Standard.JSP_PATH_PREFIX+"hotel/list.jsp" );
		request.setAttribute("hotel", hotelService.get(hotelId));
		String path = field+"/list.jsp?hotel="+hotelId;
		if (Standard.FORWARD == mode) {
			Util.forward(getServletConfig().getServletContext(), request,
					response, path, false);
		} else {
			Util.redirect(request, response, "list.jsp?hotel="+hotelId, false);
		}
	}

	private void goToAdd(HttpServletRequest request,
			HttpServletResponse response, Hotel bean) throws ServletException,
			IOException {
			Hotel hotel=(Hotel) hotelService.get(Integer.parseInt(Util.getQueryParam(request, "hotel", "")));
			request.setAttribute("hotel", hotel);
		Util.forward(getServletConfig().getServletContext(), request, response,
				field+"/add.jsp", false);
	}
}
